<?php

namespace App\Http\Controllers\Admin\UserManager;

use App\Services\Tools\Export;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

//引用http请求类
use App\Response\Response;
use Illuminate\Support\Facades\Validator;

class UserUntieLogController extends BaseController
{

    public function list(Request $request)
    {
        //参数校验
        $params = $request->only(['user_code', 'mobile', 'nickname', 'start_time', 'end_time', 'page_size', 'page']);
        $validator = Validator::make($params, [
            'user_code' => 'filled|numeric',
            'mobile'    => 'filled|numeric',
            'nickname'  => 'filled|string|max:255',
            'start_time' => 'filled|numeric',
            'end_time'   => 'filled|numeric',
            'page_size'    => 'filled|numeric',
            'page'       => 'filled|numeric',
        ], [
            'user_code.filled'  => '用户ID不能为空',
            'user_code.numeric' => '用户ID必须为数字',
        ]);
        if ($validator->fails()) {
            throw new BasicException(10001, $validator->errors()->first());
        }

        $list = app('repo_user')->getUserUntieQueryList($params);

        return Response::sendList($list);
    }

    /**
     * @api {get} /user-manager/user-ban-list/excel 用户封禁记录导出
     * @apiVersion 0.1.0
     * @apiName 会员管理-用户封禁记录导出
     * @apiGroup 会员管理
     * @apiParam {number} user_code 用户编号 可选
     * @apiParam {number} admin_id 管理员id 可选
     * @apiParam {string} mobile 用户电话 可选
     * @apiParam {string} nickname 用户昵称 可选
     * @apiParam {string} remark 封禁注释 可选
     * @apiParam {number} start_time 开始时间 可选
     * @apiParam {number} end_time 结束时间 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "uri": "asdasdasd.com"
     * }
     */
    public function toExcel(Request $request)
    {
        //参数校验
        $params = $request->only(['user_code', 'mobile', 'nickname', 'start_time', 'end_time', 'page_size', 'page']);
        $validator = Validator::make($params, [
            'user_code' => 'filled|numeric',
            'mobile'    => 'filled|numeric',
            'nickname'  => 'filled|string|max:255',
            'start_time' => 'filled|numeric',
            'end_time'   => 'filled|numeric',
            'page_size'    => 'filled|numeric',
            'page'       => 'filled|numeric',
        ], [
            'user_code.filled'  => '用户ID不能为空',
            'user_code.numeric' => '用户ID必须为数字',
        ]);
        if ($validator->fails()) {
            throw new BasicException(10001, $validator->errors()->first());
        }

        $res = app('repo_user')->getUserUntieQueryAll($request->input());


        $fields = [
            ['field' => 'id', 'header' => '序号', 'width' => '18'],
            ['field' => 'user_code', 'header' => '用户编号', 'width' => '18'],
            ['field' => 'nickname', 'header' => '昵称', 'width' => '18'],
            ['field' => 'mobile', 'header' => '用户手机号', 'width' => '18'],
            ['field' => 'seal_time', 'header' => '封禁时间', 'width' => '18'],
            ['field' => 'created_at', 'header' => '解封时间', 'width' => '18'],
            ['field' => 'reason', 'header' => '解封理由', 'width' => '80'],
            ['field' => 'admin_name', 'header' => '操作管理员', 'width' => '18'],
        ];

        $downloadUrl = Export::exportUsePhpOffice('user_untie', $fields, $res);
        app('model_admin_export_log')->add(34, $request->input(), $downloadUrl);
        return Response::sendData(['uri' => $downloadUrl]);
    }



}
